<?php

use yii\db\Migration;

class m000005_000003_create_card_bill extends Migration
{
    public function safeUp()
    {
        $this->createTable('{{%card_bill}}', [
            'id' => $this->primaryKey(),
            'user_id' => $this->integer()->comment('用户编号'),
            'card_id' => $this->integer()->notNull()->comment('卡编号'),
            'package_id' => $this->integer()->comment('套餐编号'),
            'month' => $this->string(6)->notNull()->comment('月份Ym'),
            'data' => $this->decimal(20, 4)->notNull()->defaultValue(0)->comment('流量（B）'),
            'sms' => $this->integer()->notNull()->defaultValue(0)->comment('短信（数量）'),
            'voice' => $this->integer()->notNull()->defaultValue(0)->comment('语音（分钟）'),
            'input_octets_month' => $this->decimal(20, 4)->notNull()->defaultValue(0)->comment('上行流量（B）'),
            'output_octets_month' => $this->decimal(20, 4)->notNull()->defaultValue(0)->comment('下行流量（B）'),
            'cost_money' => $this->decimal(12, 2)->notNull()->defaultValue(0)->comment('成本（上游账单金额）'),
            'money' => $this->decimal(12, 2)->notNull()->defaultValue(0)->comment('金额'),
            'create_time' => $this->integer()->notNull()->comment('创建时间'),
        ]);
        $this->createIndex('fk_card_bill_user1_idx', '{{%card_bill}}', ['user_id']);
        try {
            $this->addForeignKey('fk_card_bill_user1', '{{%card_bill}}', ['user_id'], '{{%user}}', ['id']);
        } catch (Exception $e) {
            Yii::error($e->getMessage());
        }
        $this->createIndex('fk_card_bill_card1_idx', '{{%card_bill}}', ['card_id']);
        try {
            $this->addForeignKey('fk_card_bill_card1', '{{%card_bill}}', ['card_id'], '{{%card}}', ['id']);
        } catch (Exception $e) {
            Yii::error($e->getMessage());
        }
        $this->createIndex('fk_card_bill_package1_idx', '{{%card_bill}}', ['package_id']);
        try {
            $this->addForeignKey('fk_card_bill_package1', '{{%card_bill}}', ['package_id'], '{{%package}}', ['id']);
        } catch (Exception $e) {
            Yii::error($e->getMessage());
        }
    }

    public function down()
    {
        $this->dropTable('{{%card_bill}}');
    }
}
